//
//  ShopMessageVC.m
//  sensen_ios
//
//  Created by pro on 16/2/23.
//  Copyright © 2016年 pro. All rights reserved.
//

#import "ShopMessageVC.h"
//控制器
#import "SystemMessageVC.h"//系统通知VC
#import "OrderMessageVC.h"//订单通知VC
//View
#import "MessageCell.h" // 消息单元格
//Model
#import "SystemModel.h"//系统消息Model
#import "OrderMessageModel.h"//订单消息Model
@interface ShopMessageVC () <UITableViewDataSource, UITableViewDelegate>
// 数据
@property (nonatomic, strong) NSArray *messagesAry; // 消息类型数组
@property (nonatomic,strong)NSMutableArray *systemAry;//系统通知消息数组
@property (nonatomic,strong)NSMutableArray *orederAry;//订单通知消息数组
// 视图
@property (nonatomic, strong) UITableView *tableView; // 表视图
@property (nonatomic, strong) UILabel *emptyView; // 没有信息时候的视图
@end

@implementation ShopMessageVC

#pragma mark - 视图方法
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"消息";
    self.view.backgroundColor = [UIColor whiteColor];
    _messagesAry = @[@"系统通知",@"订单通知"]; // 消息类型
    [self createLeftBarButtonItemWithImagename:nil]; // 创建左侧返回按钮
    [self _createView]; // 创建视图
    UserModel *user = [CommonCache getUserInfo];
    if (user == nil) { // 未登录
        LoginVC *vc = [[LoginVC alloc] init];
        BaseNavVC *naviVc = [[BaseNavVC alloc] initWithRootViewController:vc];
        [self presentViewController:naviVc animated:YES completion:nil];
    }
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:YES];
    [self _initData]; // 获取数据
    // 隐藏标签栏
    MainTabBarController *main = (MainTabBarController *)self.navigationController.tabBarController;
    [main hideTabBar];
}
- (void)viewWillDisappear:(BOOL)animated {
    // 显示标签栏
    MainTabBarController *main = (MainTabBarController *)self.navigationController.tabBarController;
    [main showTabBar];
}
#pragma mark - 获取数据
- (void)_initData {
    UserModel *user = [CommonCache getUserInfo];
    if (user == nil) {
        _tableView.hidden = YES;
        _emptyView.hidden = NO;
        return;
    }else{
        _tableView.hidden = NO;
        _emptyView.hidden = YES;
    }
    //请求系统通知消息
    NSDictionary *params1 = @{@"uid":user.uid,@"msg_type":@"1"};
//    NSDictionary *params1 = @{@"uid":@"21",@"msg_type":@"1"};//测试数据
    [ BoyeHttpClient post:@"BY_Message_query" apiVerson:100 params:params1 success:^(AFHTTPRequestOperation *operation, id result) {
        _systemAry = [NSMutableArray array];
        NSLog(@"%@",result);
        if (![result isKindOfClass:[NSNull class]]) {
            for (NSDictionary *dic in result) {
                NSString *title = dic[@"title"];
                NSLog(@"%@",title);
                SystemModel *systemModel = [[SystemModel alloc] initContentWithDic:dic];
                [_systemAry addObject:systemModel];
            }
        }
        [_tableView reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    }];
    //请求订单通知消息
    NSDictionary *params2 = @{@"uid":user.uid,@"msg_type":@"2"};
//    NSDictionary *params2 = @{@"uid":@"21",@msg_type":@"2"};//测试数据
    [BoyeHttpClient post:@"BY_Message_query" apiVerson:100 params:params2 success:^(AFHTTPRequestOperation *operation, id result) {
        NSLog(@"%@",result);
        _orederAry = [NSMutableArray array];
        NSLog(@"%@",result);
        if (![result isKindOfClass:[NSNull class]]) {
            for (NSDictionary *dic in result) {
                OrderMessageModel *orderModel = [[OrderMessageModel alloc] initContentWithDic:dic];
                [_orederAry addObject:orderModel];
            }
            
        }
        [_tableView reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"failure");
    }];
    }
#pragma mark - 创建视图
- (void)_createView{
    [self _initTableView];
    [self _createEmptyViewView];
}
// 创建表视图
- (void)_initTableView {
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStyleGrouped];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [_tableView setBackgroundColor:Gray_COLOR];
    [self.view addSubview:_tableView];
    // 注册单元格
    UINib *nib = [UINib nibWithNibName:@"MessageCell" bundle:nil];
    [_tableView registerNib:nib forCellReuseIdentifier:@"MessageCell"];
}
// 没有信息时候的视图
- (void)_createEmptyViewView {
    CGFloat x = 0;
    CGFloat y = 0;
    CGFloat width = 100;
    CGFloat height = 20;
    _emptyView = [[UILabel alloc] initWithFrame:CGRectMake(x, y, width, height)];
    _emptyView.font = FONT(15);
    _emptyView.text = @"暂无消息通知";
    _emptyView.textColor = [UIColor darkGrayColor];
    _emptyView.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_emptyView];
    _emptyView.center = self.view.center;
}
#pragma mark - UITableViewDataSource UITableViewDelegate
// 设置组数
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    if ([_fromNum integerValue] == 1) {
        return 1;
    }

    return _messagesAry.count;
}
// 设置行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
// 设置单元格属性
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    MessageCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MessageCell"];
    // 设置单元格不可点击
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    // 设置属性
    cell.title = _messagesAry[indexPath.section];
    if (indexPath.section == 0) {
        cell.imageType = @"消息通知";
        if (_systemAry.count == 0 ||_systemAry == nil) {
            cell.message = @"暂无消息";
        }else{
            SystemModel *systemModel = self.systemAry[0];
            NSLog(@"%@",self.systemAry[0]);
            cell.systemModel = systemModel;
        }
    }else{
        cell.imageType = @"订单通知";
        if (_orederAry.count == 0 ||_orederAry == nil) {
            cell.message = @"暂无消息";
        }else{
            OrderMessageModel *oderModel = self.orederAry[0];
            cell.oderModel = oderModel;
        }
    }
    return cell;
}
// 设置单元格行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 50;
}
// 设置组头高
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 1;
}
//设置组尾高
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5;
}
// 单元格点击事件
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
     if (indexPath.section == 0) {
        SystemMessageVC *systemVC = [[SystemMessageVC alloc] init];
        systemVC.messagesAry = self.systemAry;
        [self.navigationController pushViewController:systemVC animated:YES];
    }else if(indexPath.section == 1){
        OrderMessageVC *orderVC = [[OrderMessageVC alloc] init];
        orderVC.messagesAry = self.orederAry;
        [self.navigationController pushViewController:orderVC animated:YES];
    }
}
#pragma mark -
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

@end
